package org.demo;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author yexi
 */
public class TopicProducer {
    public static void main(String[] args) throws JMSException, InterruptedException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL
        );
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("testTopic");
        MessageProducer producer = session.createProducer(topic);
        for (int i = 0; i < 10; i++) {
            String msg = "msg" + i;
            System.out.println("发送消息: " + msg);
            TextMessage textMessage = session.createTextMessage(msg);
            producer.send(textMessage);
            session.commit();
            Thread.sleep(100);
        }
        producer.close();
        session.close();
        connection.close();
    }
}

